﻿
function operate(value, row, index) {
    var str = "冻结";
    if (row.IsLock === 1)
        str = "恢复";
    return "<a href='#' class='button-delete button-danger' id=" + row.UserID + ">删除</a> <a href='#' class='button-edit button-default' id=" + row.UserID + ">权限</a> <a href='#' class='button-status button-warning' status=" + row.IsLock + " id=" + row.UserID + ">" + str + "</a>";
}

function usertype(value, row, index) {
    var str = "";
    switch (value) {
        case 1:
            str = "学生";
            break;
        case 2:
            str = "老师";
            break;
        case 0:
            str = "其他";
            break;
    }
    return str;
}

function OperateUser(id, flag) {
    $.messager.progress();
    $.ajax({
        url: "/EasyUI/UserInfo/OperateUser",
        type: "post",
        data: {
            "__RequestVerificationToken": $("[name=__RequestVerificationToken]").val(),
            UserID: id,
            flag: flag
        },
        success: function (result) {
            $.messager.progress('close');
            if (typeof (result) !== "object")
                result = JSON.parse(result);
            if (result !== null && typeof (result) === "object") {
                if (result.code === 200) {
                    OperateMsg("温馨提示", result.msg);
                    $("#userinfo").datagrid('reload');
                } else if (result.code === 401)
                    $.messager.alert("温馨提示", result.msg, "error");
                else if (result.code === 402) {
                    ChangeImage();
                    $.messager.alert("异常提醒", result.msg, "error");
                } else
                    $.messager.alert("异常提醒", "系统异常", "error");
            } else
                $.messager.alert("异常提醒", "系统异常", "error");
        },
        error: function () {
            $.messager.progress('close');
            $.messager.alert("异常错误", "异常错误", "error");
        }
    });
}

$("#UpdateTree").click(function () {
    UpdateTree(2, $("#UpdateTree").attr("userid"));
});

$('#userinfo').datagrid({
    cls: "theme-datagrid",
    title: "用户管理",
    singleSelect: true,
    method: 'post',
    cache: false,
    rownumbers: true,
    pagination: true,
    pageSize: 10,
    collapsible: true,
    url: '/EasyUI/UserInfo/GetUserList',
    onLoadSuccess: function () {
        $('.button-delete').linkbutton({
            onClick: function () {
                OperateUser($(this).attr("id"), 1);
            }
        });

        $('.button-edit').linkbutton({
            onClick: function () {
                var url = "/EasyUI/TreeBox/GetMenuTreeJson?uid=" + $(this).attr("id");
                $('#treebox').tree({
                    url: url,
                    method: 'get',
                    checkbox: true
                });
                $("#UpdateTree").attr("userid", $(this).attr("id"));
                $('#userdialog').dialog('open');
            }
        });

        $('.button-status').linkbutton({
            onClick: function () {
                if ($(this).attr("status") === "1")
                    OperateUser($(this).attr("id"), 3);
                else
                    OperateUser($(this).attr("id"), 4);
            }
        });
    }
});


